TOP = $(shell pwd)
OUTDIR := $(TOP)/build
Generator := $(shell if [ -x /usr/bin/ninja ];then echo "Ninja"; else echo "Unix Makefiles"; fi)

all: build

prebuild:
	@mkdir -p $(OUTDIR)

build: prebuild
	@cmake -S . -B build -G "$(Generator)"
	@cmake --build build/

build-check: prebuild
	@cmake -S . -B build -G "$(Generator)" -DMEMCHECK=ON
	@cmake --build build/

release: prebuild
	@cmake -S . -B build -G "$(Generator)" -DCMAKE_BUILD_TYPE=Release
	@cmake --build build/

test: build
	@ctest -V --test-dir build

clean: prebuild
	@cmake --build build/ --target clean

disclean:
	@rm -rf $(OUTDIR)

KEY_PATH = /tmp/syralink_test_key
DIR_EXISTS := $(shell if [ -d $(KEY_PATH) ]; then echo "yes"; fi)

sm2:
ifeq ($(DIR_EXISTS),yes)
	@echo " KEY ALREADY EXISTS "
else
	mkdir -p $(KEY_PATH)
	openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:SM2 -out $(KEY_PATH)/private.pem
	openssl pkey -in $(KEY_PATH)/private.pem -pubout -out $(KEY_PATH)/public.pem
	@echo "----- PRIVATE KEY -----"
	openssl pkey -in $(KEY_PATH)/private.pem -text -noout
	@echo "----- PUBLIC KEY -----"
	openssl ec -in $(KEY_PATH)/public.pem -pubin -text -noout
endif

showsm2:
ifeq ($(DIR_EXISTS),yes)
	@echo "----- PRIVATE KEY -----"
	openssl pkey -in $(KEY_PATH)/private.pem -text -noout
	@echo "----- PUBLIC KEY -----"
	openssl ec -in $(KEY_PATH)/public.pem -pubin -text -noout
else
	@echo " KEY NOT FOUNDED "
endif
